package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 前端攻略
 */

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("")
    public String index(Model model){

        model.addAttribute("commends", strategyCommendService.getTop5());

        //页面实现: 热门推荐排行
        //hotCds
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.queryHotCommendTop10();
        model.addAttribute("hotCds", hotCds);

        //国内攻略推荐排行
        List<StrategyStatisVO>   unabroadCds = strategyStatisRedisService.queryChinaCommendTop10();
        model.addAttribute("unabroadCds", unabroadCds);
        //国外攻略推荐排行
        List<StrategyStatisVO>   abroadCds = strategyStatisRedisService.queryAbroadCommendTop10();
        model.addAttribute("abroadCds", abroadCds);


        //主题推荐

        //存在问题:
        //1:分组统计个数倒序排
        //2:关联目的地需要排重
        //3:取前面10个即可
        List<Map<String, Object>> themeCds = strategySearchService.queryThemeCommentTop10();
        model.addAttribute("themeCds", themeCds);



        //国内攻略条件:chinas
        List<StatisVO> chinas = strategySearchService.queryChinaCondition();
        model.addAttribute("chinas", chinas);
        //国外攻略条件:abroads
        List<StatisVO> abroads = strategySearchService.queryAbroadCondition();
        model.addAttribute("abroads", abroads);

        //主题攻略条件:themes
        List<StatisVO> themes = strategySearchService.queryThemeCondition();
        model.addAttribute("themes", themes);

        return "strategy/index";
    }



    @RequestMapping("detail")
    public String detail(Model model, Long id){

        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);

        model.addAttribute("detail", detail);


        //阅读数 + 1
        strategyStatisRedisService.addViewnum(id,1);

        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(id);

        model.addAttribute("vo",vo);

        //热门攻略排行 + 分
        strategyStatisRedisService.addHotScore(id,1);












        return "strategy/detail";
    }
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){

        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        Destination dest = toasts.remove(toasts.size() - 1);
        //toasts
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", dest);


        //tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags", tags);

        PageInfo page = strategyDetailService.query(qo);

        model.addAttribute("pageInfo", page);

        return "strategy/list";
    }


    //攻略评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    //@RequireLogin
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCreateTime(new Date()); //时间

        strategyCommentService.save(comment);

        //评论数 + 1
        strategyStatisRedisService.addReplynum(comment.getDetailId(), 1);

        //热门攻略排行 + 分
        strategyStatisRedisService.addHotScore(comment.getDetailId(),1);


        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(comment.getDetailId());
        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }


    //评论的列表

    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){

        //此处的page不是mybatis那个pageInfo, 而是 spring-data-jpa 里面分页对象:Page
        model.addAttribute("page", strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    //评论点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid){
        strategyCommentService.thumpUp(toid, fromid);

        return AjaxResult.SUCCESS;
    }


    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo){
        //判断当前用户是否登录
        if(userInfo == null){
            //没有登录
            AjaxResult ajax =  new AjaxResult(false,"请先登录");
            ajax.setCode(102);
            return ajax;
        }
        //约定: 如果ret true表示收藏, 如果ret false 取消收藏
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);

        //国内国外攻略推荐加分
        strategyStatisRedisService.addCommendScore(sid, ret?1:-1);

        return new AjaxResult(ret).addData(vo);
    }
    //顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){
        //判断当前用户是否登录
        if(userInfo == null){
            //没有登录
            AjaxResult ajax =  new AjaxResult(false,"请先登录");
            ajax.setCode(102);
            return ajax;
        }
        //约定: 如果ret true表示顶成功, 如果ret false 已经定过
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());

        if(ret){
            //国内国外攻略推荐加分
            strategyStatisRedisService.addCommendScore(sid, 1);
        }

        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);
        return new AjaxResult(ret).addData(vo);
    }



    //多条件查询
    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo){

        Page page = strategySearchService.query(qo);

        model.addAttribute("page", page);



        return "strategy/searchPageTpl";
    }



}
